<?php

// +----------------------------------------------------------------------
// | EasyAdmin
// +----------------------------------------------------------------------
// | PHP交流群: 763822524
// +----------------------------------------------------------------------
// | 开源协议  https://mit-license.org 
// +----------------------------------------------------------------------
// | github开源项目：https://github.com/zhongshaofa/EasyAdmin
// +----------------------------------------------------------------------

namespace app\admin\model;


use app\common\model\TimeModel;
use think\facade\Db;
use think\Model;

class MallMerchantOrders extends TimeModel
{

    // protected $table = "ea_mall_merchant_orders";

    //时间戳转换日期格式
    protected $dateFormat = 'Y-m-d H:i:s';
    protected $type = [
        'createtime'  =>  'timestamp',
    ];

    public function cate()
    {
        return '';
    }


    // 模型事件
    public static function onBeforeInsert($data)
    {


        $arr = [];
        if (count(explode(',', $data['goods_id'])) >= 2) { //要拆分的商品
            $goodsid = explode(',', $data['goods_id']);
            foreach ($goodsid as $k => $g) {
                // 生成8位随机数字
                $ordernum =  time() . str_pad(mt_rand(1, 99999999), 8, '0', STR_PAD_LEFT);;

                //查询要拆分商品的初始价格
                $price = MallGoods::where('id', $g)->find();
                $price = $price->toArray();

                //拆分的单个订单
                $arr3 = [
                    'ordernum' => $ordernum,
                    'goods_id' => $g,
                    'goods' => $price['title'],
                    'bug_num' => $data['bug_num'],
                    'user_id' => $data['user_id'],
                    'shop_id' => $price['shop_id'],
                    'price' => $price['discount_price'],
                    'status' => $data['status'],
                    'createtime' => $data['createtime'],
                ];

                //把单个订单数组装进数组
                array_push($arr, $arr3);
            }
        } else {
            $goods_id = MallGoods::where('id', $data['goods_id'])->find();
            $goods_id = $goods_id->toArray();
            $arr3 = [
                'ordernum' => $data['ordernum'],
                'goods_id' => $goods_id['id'],
                'goods' => $goods_id['title'],
                'bug_num' => $data['bug_num'],
                'user_id' => $data['user_id'],
                'shop_id' => $goods_id['shop_id'],
                'price' => $goods_id['discount_price'],
                'status' => $data['status'],
                'createtime' => $data['createtime'],
            ];
            array_push($arr, $arr3);
        }
        return $arr;
    }

    // 数据库事务
    public static function createOrder($data)
    {
        // 启动事务
        Db::startTrans();
        try {
            // 新增订单
            foreach ($data as $order) {
                $order = static::onBeforeInsert($order);
                // echo '<pre>';
                // print_r($order);
                // static::create($order);
                $add = new MallMerchantorders();
                $add->saveAll($order);
            }
            // 提交事务
            Db::commit();
        } catch (\Exception $e) {
            // 回滚事务
            Db::rollback();
            throw new \Exception('新增数据失败：' . $e->getMessage());
        }
    }
}
